package org.ucas.kgraph.dto;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Defaults;

import freemarker.template.Template;

import org.hibernate.type.descriptor.java.JdbcTimestampTypeDescriptor.TimestampMutabilityPlan;
import org.springframework.http.HttpStatus;
import org.ucas.kgraph.dto.GraphDto.Node;
import org.ucas.kgraph.entity.File;
import org.ucas.kgraph.entity.Schem;
import org.ucas.kgraph.utils.exception.NotFoundException;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

public class FileDto {

  public String id;

  public String text;

  public List<FileDto> nodes;
 
  public String getId() {
	return id;
  }

  public void setId(String id) {
	this.id = id;
  }

  public String getText() {
	return text;
  }

  public void setText(String text) {
	this.text = text;
  }

  public List<FileDto> getNodes() {
	return nodes;
  }

  public void setNodes(List<FileDto> nodes) {
	this.nodes = nodes;
  }
 
  public static FileDto of(List<File> fileList) {
	  return addByParentID(Long.valueOf(0),fileList);
  }

  private static FileDto addByParentID(Long parent_id,List<File> fileList) {
	// TODO Auto-generated method stub
	FileDto tmp=new FileDto();
	if(parent_id.equals(Long.valueOf(0))){
		tmp.setId("filelist-"+parent_id.toString());
		tmp.setText("rootName");
	}
	tmp.setNodes(new ArrayList<>());
	for(int i = 0; i < fileList.size(); i++)
	{
		if(fileList.get(i).getId().equals(parent_id)&&!parent_id.equals(Long.valueOf(0))){
			
			tmp.setId("filelist-"+parent_id.toString());
			tmp.setText(fileList.get(i).getName());
		}
		
		if(fileList.get(i).getParent_id().equals(parent_id)){
			
			tmp.getNodes().add(addByParentID(fileList.get(i).getId(),fileList));
		}
		
	}
	return tmp;
  }
  
  public static FileDto ofroot(File file){
	  FileDto fileDto = new FileDto();
	  fileDto.id=file.getId().toString();
	  fileDto.text=file.getName();
	  return fileDto;
  }
}
